package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.springframework.util.StringUtils;

import java.io.*;

/**
 * @author wangfupeng
 */
public class MybatisUtilFeignClient {

    public static void writeFeignClient(String eurekaServiceName, String feignClientUrl, String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String servicePackage) {
        File file = null;
        OutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            String packageNameFolder = basePackageName.replaceAll("\\.", "/");
            targetProject = targetProject.replaceAll("\\./", "");
            File folder = new File(targetProject + "/" + packageNameFolder + "/feign/common/client");
            folder.mkdirs();
            file = new File(targetProject + "/" + packageNameFolder + "/feign/common/client/" + tableName + "Client.java");
            //如果文件不存在，则创建文件
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
            bufferedWriter.write("package " + basePackageName + ".feign.common.client;\n" +
                    "\n" +
                    "import org.springframework.cloud.netflix.feign.FeignClient;\n" +
                    "import org.springframework.web.bind.annotation.RequestHeader;\n" +
                    "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                    "import org.springframework.web.bind.annotation.RequestMethod;\n" +
                    "import org.springframework.web.bind.annotation.RequestParam;\n" +
                    "import org.springframework.web.bind.annotation.RequestBody;\n" +
                    "import org.springframework.web.bind.annotation.PathVariable;\n" +
                    "import " + basePackageName + ".controller.response.ListResponse;\n" +
                    "import " + basePackageName + ".controller.response.SimpleResponse;\n" +
                    "import io.swagger.annotations.ApiParam;\n" +
                    "import org.springframework.http.MediaType;\n" +
                    "import " + basePackageName + ".bean.model." + tableName + ";\n" +
                    "import java.util.Date;\n" +
                    "\n" +
                    "\n" +
                    "@FeignClient(name = \"" + eurekaServiceName + "\""+(StringUtils.isEmpty(feignClientUrl)?"":", url = \""+feignClientUrl+"\"")+")\n" +
                    "public interface " + tableName + "Client {\n" +
                    "\n" +
                    "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/add\", method = RequestMethod.POST)\n" +
                    "    public SimpleResponse add(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, @RequestBody " + tableName + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + ");\n" +
                    "\n");
            String pathForPrimaryKeyColumns = MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable);
            if (!"".equals(pathForPrimaryKeyColumns)) {
                bufferedWriter.write("    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/delete"+MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable)+"\", method = RequestMethod.GET)\n" +
                        "    public SimpleResponse delete(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, ");
                bufferedWriter.write(MybatisUtilCommon.getParameterWithAnnotationForPrimaryKeyColumns(introspectedTable));
                bufferedWriter.write(");\n" +
                        "\n");
                if (MybatisUtilCommon.getExistNonePrimaryKeyColumns(introspectedTable)) {
                    bufferedWriter.write("    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/update\", method = RequestMethod.POST)\n" +
                            "    public SimpleResponse update(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, @RequestBody "+tableName+" "+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) +");\n" +
                            "\n" +
                            "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/get"+MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable)+"\", method = RequestMethod.GET)\n" +
                            "    public SimpleResponse get(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, "+MybatisUtilCommon.getParameterWithAnnotationForPrimaryKeyColumns(introspectedTable)+");\n" +
                            "\n" +
                            "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/mock"+MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable)+"\", method = RequestMethod.GET)\n" +
                            "    public SimpleResponse mock(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, "+MybatisUtilCommon.getParameterWithAnnotationForPrimaryKeyColumns(introspectedTable)+");\n" +
                            "\n");
                }
            }
            bufferedWriter.write("    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/list\", method = RequestMethod.GET)\n" +
                    "    public ListResponse list(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, @ApiParam(value = \"Admin 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\")  Integer page, @ApiParam(value = \"Admin 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"20\") Integer limit, @ApiParam(value = \"Admin 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id asc\") String orderBy);\n" +
                    "    \n" +
                    "\n" +
                    "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/listMockWithoutAuthUser\", method = RequestMethod.GET)\n" +
                    "    public ListResponse listMockWithoutAuthUser(@ApiParam(value = \"Admin 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\")  Integer page, @ApiParam(value = \"Admin 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"20\") Integer limit, @ApiParam(value = \"Admin 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id asc\") String orderBy);\n" +
                    "\n" +
                    "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/listWithoutCache\", method = RequestMethod.GET)\n" +
                    "    public ListResponse listWithoutCache(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, @ApiParam(value = \"Admin 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\")  Integer page, @ApiParam(value = \"Admin 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"20\") Integer limit, @ApiParam(value = \"Admin 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id asc\") String orderBy);\n" +
                    "\n" +
                    "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/listWithMock\", method = RequestMethod.GET)\n" +
                    "    public ListResponse listWithMock(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, @ApiParam(value = \"Admin 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\")  Integer page, @ApiParam(value = \"Admin 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"20\") Integer limit, @ApiParam(value = \"Admin 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id asc\") String orderBy);\n" +
                    "\n" +
                    "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/query\", method = RequestMethod.GET)\n" +
                    "    public ListResponse query(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, @ApiParam(value = \"Admin 页码\") @RequestParam(value = \"page\", required = false, defaultValue = \"1\")  Integer page, @ApiParam(value = \"Admin 一页返回条数\") @RequestParam(value = \"limit\", required = false, defaultValue = \"20\") Integer limit, @ApiParam(value = \"Admin 排序模式\") @RequestParam(value = \"orderBy\", required = false, defaultValue = \"id asc\") String orderBy, "+MybatisUtilCommon.getParameterWithAnnotationForColumnsWithMaxMinPrefixSuffixLike(introspectedTable)+");\n" +
                    "\n" +
                    "    @RequestMapping(value = \"/feign/v1/common/"+MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName)+"/distinct\", method = RequestMethod.GET)\n" +
                    "    public SimpleResponse distinct(@ApiParam(value = \"token\")  @RequestHeader(value=\"token\", required = false) String token, @ApiParam(value = \"查询字段\") @RequestParam(value = \"field\", required = true, defaultValue = \"id\")  String field);\n" +
                    "\n" +
                    "\n}" +
                    "\n"
            );
            System.out.println("write to file success : " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
